/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import pojo.PhieuThu;
import util.HibernateUtil;

/**
 *
 * @author LaiHuynh
 */
public class PhieuThuDAO {
    public static List<PhieuThu> layDanhSachPhieuThu() {
        List<PhieuThu> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select pt from PhieuThu pt";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }   
        return ds;        
    }
    
    public static PhieuThu layThongTinPhieuThu(int maPhieuThu) {
        PhieuThu pt = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            pt = (PhieuThu) session.get(PhieuThu.class, maPhieuThu);
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return pt;
    }
    
    public static void themPhieuThu(PhieuThu pt) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(pt);
        session.getTransaction().commit();
        session.close(); 
    }
    
    public static boolean xoaPhieuThu(int maPhieuThu) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        PhieuThu pt = PhieuThuDAO.layThongTinPhieuThu(maPhieuThu); 
        if(pt==null){ 
            return false;
        } 
        session.beginTransaction();
        session.delete(pt);
        session.getTransaction().commit();
        session.close(); 
        return true; 
    }
    
    public static void capNhatThongTinHangsx(PhieuThu pt) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(pt);
        session.getTransaction().commit();
        session.close(); 
    }
}
